import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class TabBarControlPage extends StatefulWidget {
  const TabBarControlPage({Key? key}) : super(key: key);

  @override
  _TabBarControlPageState createState() => _TabBarControlPageState();
}

class _TabBarControlPageState extends State<TabBarControlPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void dispose() {
    //生命周期函数
    // TODO: implement dispose
    super.dispose();
    _tabController.dispose();
  }

  @override
  void initState() {
    //生命周期函数
    // TODO: implement initState
    super.initState();
    _tabController = new TabController(length: 4, vsync: this);
    _tabController.addListener(() {
      print('当前的索引值${_tabController.index}');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('TabBarControlPage'),
        bottom: TabBar(
          controller: this._tabController,
          tabs: [
            Tab(
              text: "热销款1",
            ),
            Tab(
              text: "热销款2",
            ),
            Tab(
              text: "热销款3",
            ),
            Tab(
              text: "热销款4",
            )
          ],
        ),
      ),
      body: TabBarView(
        controller: _tabController,
        children: [
          Center(
            child: Text("热销1"),
          ),
          Center(
            child: Text("热销2"),
          ),
          Center(
            child: Text("热销3"),
          ),
          Center(
            child: Text("热销4"),
          ),
        ],
      ),
    );
  }
}
